const path = require('path')
const { APP_PORT, SYS_LOCATION } = require('../config/config.development');
const { goodsAdd, goodsUpdate, goodsDelete, goodsDetail, goodsList, getGoods } = require('../service/goods.service')
class GoodsController {
    // 商品图片上传
    async upload(ctx) {
        ctx.body = "图片上传成功!"
        const { file } = ctx.request.files
        const fileType = ['image/jpeg', 'image/png', 'image/gif']
        if (file) {
            if (!fileType.includes(file.mimetype)) {
                return ctx.body = {
                    code: 500,
                    message: '图片格式不正确!',
                    data: ""
                }
            } else {
                ctx.body = {
                    code: 200,
                    message: '图片上传成功!',
                    data: {
                        image: `http://${SYS_LOCATION}:${APP_PORT}/${path.basename(file.filepath)}`
                    }
                }
            }
        } else {
            ctx.body = {
                code: 500,
                message: '图片上传失败!',
                data: ""
            }
        }
    }
    // 发布商品
    async addGoods(ctx) {
        try {
            const findresult = await getGoods(ctx.request.body.goodsname)
            if (findresult) {
                ctx.body = {
                    code: 500,
                    message: '商品已存在!',
                    data: ""
                }
                return
            } else {
                const res = await goodsAdd(ctx.request.body)
                ctx.body = {
                    code: 200,
                    message: '添加成功!',
                    data: res
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '添加失败!',
                data: err
            }
            return
        }
    }
    // 修改商品信息
    async updateGoods(ctx) {
        try {
            const res = await goodsUpdate(ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '更新成功!',
                    data: ''
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '更新失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '更新失败!',
                data: err
            }
            return
        }
    }
    // 删除商品
    async deleteGoods(ctx) {
        try {
            const res = await goodsDelete(ctx.request.body.id)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '删除成功!',
                    data: ''
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '删除失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '删除失败!',
                data: err
            }
        }
    }
    // 获取详情
    async getGoodsDetail(ctx) {
        try {
            const res = await goodsDetail(ctx.request.body.id)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '获取详情成功!',
                    data: res
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '获取详情失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '获取详情失败!',
                data: err
            }
        }
    }
    // 获取商品列表
    async getGoodsList(ctx) {
        try {
            const { res, total } = await goodsList(ctx.request.body.page, ctx.request.body.pageSize, ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '获取列表成功!',
                    data: res,
                    total: total
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '获取列表失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '获取列表失败!',
                data: err
            }
        }
    }
}
module.exports = new GoodsController()